﻿using DGSFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Framework.WebmasterTool
{
    public partial class Schedule : System.Web.UI.Page
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckPermission();
                LoadData();
            }

        }

        public void CheckPermission()
        {
            MembershipUser someUser = Membership.GetUser();
            if (someUser == null)
            {
                Response.Redirect(Globals.ApplicationPath + "LoginAdmin/Default.aspx");
            }
            bool flag = false;
            var user = entity.aspnet_Users.Where(us => us.UserName == someUser.UserName).FirstOrDefault();
            user.aspnet_Roles.Load();
            var rolesForUser = user.aspnet_Roles.ToList();
            foreach (var item in rolesForUser)
            {
                var permission = entity.udv_Permissions.Where(ps => ps.RoleId == item.RoleId).FirstOrDefault();
                if (permission.Article.Value == true)
                {
                    flag = true;
                }
            }
            if (flag == false)
            {
                lblPermissionMessage.Visible = true;
                pnPermission.Visible = false;
            }
        }
        public void LoadData()
        {
            try
            {
                var schedule = new List<dgs_Schedule>();
                var yearSearch = txtYear.Text;
                if (!string.IsNullOrEmpty(yearSearch))
                {
                    var year = int.Parse(yearSearch);
                    schedule = entity.dgs_Schedule.Where(d => d.ProcessYear == year).OrderByDescending(d => d.DateCreate).ToList();
                }
                else
                {
                    schedule = entity.dgs_Schedule.OrderByDescending(d => d.DateCreate).ToList();
                }
                grvArticles.DataSource = schedule;
                grvArticles.DataBind();
            }
            catch (Exception ex)
            {

            }

        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                var schedule = new dgs_Schedule();
                var dateStr = txtDate.Text;
                var dateSplit = dateStr.Split('/');
                var processDate = DateTime.Parse(dateSplit[2] + "/" + dateSplit[1] + "/" + dateSplit[0]);
                schedule.ProcessDate = processDate;
                schedule.ProcessMonth = processDate.Month;
                schedule.ProcessYear = processDate.Year;
                schedule.CreateBy = Membership.GetUser().UserName;
                schedule.DateCreate = DateTime.Now;
                schedule.Note = txtNote.Text;
                entity.AddTodgs_Schedule(schedule);
                entity.SaveChanges();
                txtDate.Text = "";
                txtNote.Text = "";
                LoadData();
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        protected void grvArticles_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvArticles.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            pnAddnew.Visible = true;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in grvArticles.Rows)
            {
                bool isChecked = ((HtmlInputCheckBox)row.FindControl("chkSelect")).Checked;
                if (isChecked)
                {
                    int scheduleID = int.Parse(((HtmlInputCheckBox)row.FindControl("chkSelect")).Attributes["title"].Trim());
                    var schedule = entity.dgs_Schedule.Where(ar => ar.ScheduleID == scheduleID).FirstOrDefault();
                    if (schedule != null)
                    {
                        entity.DeleteObject(schedule);
                        entity.SaveChanges();
                    }
                }
            }
            LoadData();
        }
    }
}